package com.arvato.wxwork.service.staff;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.arvato.common.https.WxworkMiniproOwnRequestUtil;
import com.arvato.common.util.JsonUtil;
import com.arvato.wxwork.bean.bean_dto.WxworkDepartmentNodeDto;


@Service
@Transactional
public class WxworkDepartmentService {
	
	@Autowired
	private WxworkMiniproOwnRequestUtil wxworkMiniproOwnRequestUtil;
	
	@Autowired
	private JsonUtil jsonUtil;
	/**
	 * 查询部门列表 递归解析 返回给前台树形结构需要的json
	 * @param departmentId
	 * @return
	 */
	public List<WxworkDepartmentNodeDto> getDepartmentTree(int departmentId){
		String accessToken = wxworkMiniproOwnRequestUtil.getMaillistAccessToken();
		if(accessToken == null) {
			throw new RuntimeException("获取accessToken失败");
		}
		Map<String, Object> departmentMap = wxworkMiniproOwnRequestUtil.departmentList(accessToken, departmentId);
		// 递归解析这个departmentMap 拼接成一个json 先查出父级ID为零的节点
		if(!"0".equals(departmentMap.get("errcode") + "")) {
			throw new RuntimeException("获取部门tree失败" + jsonUtil.obj2String(departmentMap));
		}
		@SuppressWarnings("unchecked")
		List<Map<String, Object>> departmentMapList = (List<Map<String, Object>>)departmentMap.get("department");
		List<WxworkDepartmentNodeDto> departmentList = new ArrayList<WxworkDepartmentNodeDto>();
		for(Map<String, Object> map : departmentMapList) {
			WxworkDepartmentNodeDto item = new  WxworkDepartmentNodeDto();
			item.setId(Integer.valueOf(map.get("id") + ""));
			item.setName(map.get("name") + "");
			item.setPid(Integer.valueOf(map.get("parentid") + ""));
			departmentList.add(item);
		}
		return departmentList;
	}
	
	/**
	 * 根据部门code 查询部门ID
	 * @param departmentCode
	 * @return
	 */
	public int getDepartmentIdByName(String departmentCode) {
		List<WxworkDepartmentNodeDto> departmentList = getDepartmentTree(1);
		for(WxworkDepartmentNodeDto departmentDto : departmentList) {
			String nodeName = departmentDto.getName();
			if(nodeName.contains("(") && nodeName.contains(")")) {
				String oldCode = departmentDto.getName().split("\\(")[1].split("\\)")[0];
				if(oldCode.equals(departmentCode)) {
					return departmentDto.getId();
				}
			}
		}
		return 1;
	}
	
	/**
	 * 新增部门 (部门的ID由企业微信自动生成)
	 * @param name
	 * @param parentid
	 * @return
	 */
	public int create(String name, int parentid) {
		String accessToken = wxworkMiniproOwnRequestUtil.getMaillistAccessToken();
		if(accessToken == null) {
			throw new RuntimeException("获取accessToken失败");
		}
		Map<String, Object> resultMap = wxworkMiniproOwnRequestUtil.createDepartment(accessToken, name, parentid);
		if(!"0".equals(resultMap.get("errcode") + "")) {
			throw new RuntimeException("创建部门失败" + jsonUtil.obj2String(resultMap));
		}
		return 1; 
	}

	/**
	 * 更新部门名称
	 * @param id
	 * @param name
	 * @return
	 */
	public int update(int id, String name) {
		String accessToken = wxworkMiniproOwnRequestUtil.getMaillistAccessToken();
		if(accessToken == null) {
			throw new RuntimeException("获取accessToken失败");
		}
		Map<String, Object> resultMap = wxworkMiniproOwnRequestUtil.updateDepartment(accessToken, id, name);
		if(!"0".equals(resultMap.get("errcode") + "")) {
			throw new RuntimeException("更新部门失败" + jsonUtil.obj2String(resultMap));
		}
		return 1;
	}
	
	/**
	 * 删除部门
	 * @param id
	 * @return
	 */
	public int delete(int id) {
		String accessToken = wxworkMiniproOwnRequestUtil.getMaillistAccessToken();
		if(accessToken == null) {
			throw new RuntimeException("获取accessToken失败");
		}
		Map<String, Object> resultMap = wxworkMiniproOwnRequestUtil.deleteDepartment(accessToken, id);
		if(!"0".equals(resultMap.get("errcode") + "")) {
			throw new RuntimeException("更新部门失败" + jsonUtil.obj2String(resultMap));
		}
		return 1;
	}

	
	
}
